package demo1;

import java.util.Arrays;

public class MyArrayList implements IArrayList {
    private int useSize;
    private int[] elem;

    public MyArrayList() {
        this.elem = new int[Constant.DEFAULT_CAPACITY];
    }


    @Override//新增元素,默认在数组最后新增
    public void add(int data) {
        //1、是否能继续存放数据，不能就进行扩容
        if(isFill()) {
            grow();
        }
        //2、在数组末尾添加数据
        this.elem[useSize] = data;
        this.useSize++;

    }

    @Override//在 pos 位置新增元素
    public void add(int pos, int data) {
        //1、是不是满的
        if(isFill()) {
            grow();
        }
        //2、检查pos位置的合法性
        checkPosAdd(pos);
        //3、移动数据
        for (int i = useSize - 1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        /*if (useSize - pos >= 0) {
            System.arraycopy(elem, pos, elem, pos + 1, useSize - pos);
        }*/
        elem[pos] = data;
        useSize++;
    }
    private void checkPosAdd(int pos) {
        if(pos < 0 || pos > this.useSize) {
            throw new PosException(Constant.ADD_CHECK_MASSAGE);
        }
    }

    @Override//判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override//查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return 0;
    }

    @Override//获取 pos 位置的元素
    public int get(int pos) {
        //1、判断顺序表是否为空
        if(isEmpty()) {
            throw new isEmptyException(Constant.EMPTY_MASSAGE);
        }
        //2、判断pos位置是否合法
        checkPos(pos);

        return elem[pos];
    }
    private void checkPos(int pos) {
        if(pos < 0 || pos >= this.useSize) {
            throw new PosException(Constant.GET_CHECK_MASSAGE);
        }
    }
        @Override//给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        if(isEmpty()) {
            throw new isEmptyException(Constant.EMPTY_MASSAGE);
        }
        checkPos(pos);
        elem[pos] = value;
    }

    @Override//删除第⼀次出现的关键字key
    public void remove(int toRemove) {
        if(isEmpty()) {
            throw new isEmptyException(Constant.EMPTY_MASSAGE);
        }
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有你要删除的数据");
            return;
        }
        for (int i = index; i < this.useSize - 1; i++) {
            elem[i] = elem[i+1];
        }
        elem[useSize - 1] = 0;
        useSize--;
        //
    }

    @Override//获取顺序表⻓度
    public int size() {
        return this.useSize;
    }

    @Override//清空顺序表
    public void clear() {
        for (int i = 0; i < this.useSize; i++) {
            this.elem[i] = 0;
        }
        this.useSize = 0;
    }

    //是否为空
    private boolean isEmpty() {
        return useSize == 0;
    }
    //是否存满
    private boolean isFill() {
        return this.useSize == this.elem.length;//判断有效数据是否等于总数组长度
    }
    //数据扩容
    private void grow() {
        this.elem = Arrays.copyOf(this.elem,elem.length*2);
    }

    @Override
    public String toString() {
        return Arrays.toString(elem);
    }
}
